﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KerriganDataContract.Forum;
using KerriganDataContract;
using System.Diagnostics;
using KerriganDataContract.Person;

namespace KerriganService.DAC
{
    public class ForumDAC
    {

        public static List<vwGetForumCategory> GetCategoryToDisplay()
        {

            List<vwGetForumCategory> entity = new List<vwGetForumCategory>();


            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity = db.vwGetForumCategories.ToList();

                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
            return entity;

        }

        public static List<vwGetForumTopic> GetTopicToDisplay(int id)
        {

            List<vwGetForumTopic> entity = new List<vwGetForumTopic>();


            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity = db.vwGetForumTopics.Where(f => f.IdForumCategoryFK == id).ToList();

                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
            return entity;


    }



        public static List<vwGetForumMessage> GetMessageToDisplay(int id)
        {

            List<vwGetForumMessage> entity = new List<vwGetForumMessage>();


            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity = db.vwGetForumMessages.Where(f => f.IdForumTopicFK == id).ToList();

                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
            return entity;

        }


        public static void AddCategory(ForumCategoryDC bo) {

            TForumCategory entity = new TForumCategory();

            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity.Description = bo.Description;
                    entity.Name = bo.NameCategory;

                    db.TForumCategories.AddObject(entity);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
        }


        public static void AddTopicAndMessage(ForumMessageDC bo)
        {

            TForumTopic entity = new TForumTopic();
            TForumMessage entityMessage = new TForumMessage();
            ForumTopicDC boTopic = new ForumTopicDC();

            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    //entity.Title = bo.Topic.Title;
                    entity.Title = bo.Topic.Title;
                    entity.Date = DateTime.Now;
                    entity.Views = 0;
                    entity.IdForumCategoryFK = bo.Topic.ForumCategory.IdForumCategory;
                    db.TForumTopics.AddObject(entity);
                    
                    db.SaveChanges();
                    int id =entity.IdForumTopic;

                    entityMessage.IdForumTopicFK = id;
                    entityMessage.MainMessage = true;
                    entityMessage.Message = bo.Message;
                    entityMessage.WrittenDate = DateTime.Now;
                    entityMessage.Person = bo.Person.IdPerson;
                    db.TForumMessages.AddObject(entityMessage);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
        }

        public static void AddMessage(ForumMessageDC bo)
        {

            TForumMessage entity = new TForumMessage();

            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity.IdForumTopicFK = bo.Topic.IdTopic;
                    entity.Message = bo.Message;
                    entity.MainMessage = bo.MainMessage;
                    entity.WrittenDate = bo.Date;
                    entity.Person = bo.Person.IdPerson;
                    db.TForumMessages.AddObject(entity);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
        }

        public static List<vwGetForumTopicByPerson> GetTopicByPerson(int idTopic , int idPerson)
        {

            List<vwGetForumTopicByPerson> entity = new List<vwGetForumTopicByPerson>();


            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity = db.vwGetForumTopicByPersons.Where(f => f.IdForumCategoryFK == idTopic && f.MainMessage == true).ToList();

                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
            return entity;

        }

        public static ForumTopicDC GetTopicById(int id)
        {
            TForumTopic entity = new TForumTopic();
            ForumTopicDC bo = new ForumTopicDC();

            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity = db.TForumTopics.FirstOrDefault(f => f.IdForumTopic == id);
                    bo = ForumDAC.CopyEntityToBo(entity);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

            return bo;
        }

        public static ForumMessageDC GetMessageById(int id)
        {
            TForumMessage entity = new TForumMessage();
            ForumMessageDC bo = new ForumMessageDC();

            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity = db.TForumMessages.FirstOrDefault(f => f.IdForumMessage == id);
                    bo = ForumDAC.CopyEntityToBoMessage(entity);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

            return bo;
        }

        public static ForumCategoryDC GetCategoryById(int id)
        {
            TForumCategory entity = new TForumCategory();
            ForumCategoryDC bo = new ForumCategoryDC();

            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity = db.TForumCategories.FirstOrDefault(f => f.IdForumCategory == id);
                    bo = ForumDAC.CopyEntityToBoCategory(entity);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

            return bo;
        }

        private static ForumCategoryDC CopyEntityToBoCategory(TForumCategory entity)
        {
            ForumCategoryDC bo = new ForumCategoryDC();

            try
            {
                bo.IdForumCategory = entity.IdForumCategory;
                bo.NameCategory = entity.Name;
                bo.Description = entity.Description;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return bo;
        }

        private static ForumMessageDC CopyEntityToBoMessage(TForumMessage entity)
        {
            ForumMessageDC bo = new ForumMessageDC();

            try
            {
                bo.Message = entity.Message;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return bo;
        }

        private static ForumTopicDC CopyEntityToBo(TForumTopic entity)
        {
            ForumTopicDC bo = new ForumTopicDC();

            try
            {
                bo.IdTopic = entity.IdForumTopic;
                bo.Title = entity.Title;
                bo.ForumCategory = new ForumCategoryDC();
                bo.ForumCategory.IdForumCategory = entity.IdForumCategoryFK;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return bo;
        }

        public static void UpdateTopic(int id, ForumDC bo)
        {

            TForumTopic entity = new TForumTopic();


            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity = db.TForumTopics.FirstOrDefault(f => f.IdForumTopic == id);
                    ForumDAC.UpdateEntityFromBo(bo, entity);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

        }

        public static void UpdateMessage(int id, ForumDC bo)
        {

            TForumMessage entity = new TForumMessage();


            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity = db.TForumMessages.FirstOrDefault(f => f.IdForumMessage == id);
                    ForumDAC.UpdateEntityFromBoMessage(bo, entity);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

        }

        private static TForumMessage UpdateEntityFromBoMessage(ForumDC bo, TForumMessage entity)
        {


            try
            {
                entity.Message = bo.Message.Message;
                entity.WrittenDate = bo.Message.Date;

            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return entity;
        }
 

        private static TForumTopic UpdateEntityFromBo(ForumDC bo, TForumTopic entity)
        {


            try
            {
                entity.Title = bo.Topic.Title;
                entity.IdForumCategoryFK = bo.Topic.ForumCategory.IdForumCategory;

            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return entity;
        }



        public static void UpdateCategory(int id, ForumDC bo)
        {

            TForumCategory entity = new TForumCategory();


            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity = db.TForumCategories.FirstOrDefault(f => f.IdForumCategory == id);
                    ForumDAC.UpdateEntityFromBoCategory(bo, entity);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

        }


        private static TForumCategory UpdateEntityFromBoCategory(ForumDC bo, TForumCategory entity)
        {


            try
            {
                entity.Name = bo.Category.NameCategory;
                entity.Description = bo.Category.Description;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return entity;
        }



        public static void DeleteCategory(int id)
        {

            TForumCategory entity = new TForumCategory();
            List<TForumTopic> entityTopic = new List<TForumTopic>();
            List<TForumMessage> entityMessage = new List<TForumMessage>();

            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity = db.TForumCategories.FirstOrDefault(f => f.IdForumCategory== id);
                    entityTopic = db.TForumTopics.Where(f => f.IdForumCategoryFK == id).ToList();
                    
                    db.TForumCategories.DeleteObject(entity);
                    if (entityTopic != null)
                    {
                        foreach (TForumTopic entities in entityTopic)
                        {
                            entityMessage = db.TForumMessages.Where(f => f.IdForumTopicFK == entities.IdForumTopic).ToList();
                            if (entityMessage != null)
                            {
                                foreach (TForumMessage entities2 in entityMessage)
                                {
                                    db.TForumMessages.DeleteObject(entities2);
                                }
                            }
                            db.TForumTopics.DeleteObject(entities);
                        }
                    }



                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

        }

        public static void DeleteTopic(int id)
        {

            TForumTopic entityTopic = new TForumTopic();
            List<TForumMessage> entityMessage = new List<TForumMessage>();

            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entityTopic = db.TForumTopics.FirstOrDefault(f => f.IdForumTopic == id);
                    entityMessage = db.TForumMessages.Where(f => f.IdForumTopicFK == entityTopic.IdForumTopic).ToList();
                    db.TForumTopics.DeleteObject(entityTopic);
                    foreach (TForumMessage entities in entityMessage)
                    {
                        db.TForumMessages.DeleteObject(entities);
                    }

                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

        }


        public static Boolean DeleteMessage(int id)
        {

            TForumTopic entityTopic = new TForumTopic();
            List<TForumMessage> entityMessage = new List<TForumMessage>();
            TForumMessage entity = new TForumMessage();
            Boolean result = false;
            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    
                    
                    entity = db.TForumMessages.Where(f => f.IdForumMessage == id).FirstOrDefault();

                    if (entity.MainMessage == true)
                    {
                        entityMessage = db.TForumMessages.Where(f => f.IdForumTopicFK == entity.IdForumTopicFK).ToList();
                        foreach (TForumMessage entities in entityMessage)
                        {
                            db.TForumMessages.DeleteObject(entities);
                        }
                        entityTopic = db.TForumTopics.FirstOrDefault(f => f.IdForumTopic == entity.IdForumTopicFK);
                        db.TForumTopics.DeleteObject(entityTopic);
                        db.SaveChanges();
                        result = true;
                    }
                    else {

                        db.TForumMessages.DeleteObject(entity);
                        db.SaveChanges();
                        result = false;
                    }

                    
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
            return result;
        }


        public static void UpdateViews(int id)
        {

            TForumTopic entity = new TForumTopic();


            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity = db.TForumTopics.FirstOrDefault(f => f.IdForumTopic == id);
                    ForumDAC.UpdateEntityFromBoViews(entity);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

        }

        private static TForumTopic UpdateEntityFromBoViews(TForumTopic entity)
        {

            try
            {
                entity.Views = entity.Views + 1;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return entity;
        
        }
    }
}